#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

Date: July 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

A GUI implementation using GTK+.

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess
import string

# Import GTK libraries
try:
  import pygtk
  #tell pyGTK, if possible, that we want GTKv2
  pygtk.require("2.0")
except:
  #Some distributions come with GTK2, but not pyGTK
  pass
try:
  import gtk
  import gtk.glade
  import gobject
except:
  print "You need to install pyGTK or GTKv2 ",
  print "or set your PYTHONPATH correctly."
  print "try: export PYTHONPATH=",
  print "/usr/local/lib/python2.2/site-packages/"
  sys.exit(1)

# Application imports
import utils
import gtk_options
import gtk_modes
import gtk_filters
import gtk_agc
import gtk_rx_proc
import gtk_radio
import gtk_vfo

#======================================================================
# The GUI class. There will be other classes downstream.
class AppGui:
  
    #---------------------------------------------------------------------
    def __init__(self, glade_path, persist_service, lc_pub, hw_pub, dsp_pub, upd_pub, ice_config, init):
        # Allow threads, without this the gui will hang all python threads
        # We don't actually use any Python threads right now but its very
        # disconcerting if you do and find they block, so leave this in
        gtk.gdk.threads_init()
        # This is how we send events into the system
        self.persist = persist_service
        self.hw_pub = hw_pub
        self.dsp_pub = dsp_pub
        self.lc_pub = lc_pub
        self.upd_pub = upd_pub
        self.ice_config = ice_config
        self.vr_data = init
        # Initialise the glade built ui
        # Load the main window
        self.entered = False # Not in the window
        mainwindow = 'TxWindow'
        
        # This loads a widget tree from the XML definition in the glade file
        self.main_tree=gtk.glade.XML (glade_path, mainwindow)
        
        # Get and show the main window
        self.w=self.main_tree.get_widget(mainwindow)
        self.w.show_all()
      
        # Set to true if we are quiting
        self.quitall = False
        
        # Wire up the top level window events
        # All events are set up in Glade, just match the event names here
        dic = { 
                }
        
        # This will connect up all the events in one hit
        self.main_tree.signal_autoconnect (dic)
        
        # Now we need a reference to all the controls
        # All controls in the tree must have a unique name
        
        # Avoid circular events
        # One issue with messaging is that a lot of controls generate an event when they
        # are changed programmatically. So you hit a button, it sends a message, then a 
        # message comes back that causes the button state to change which causes it to 
        # generate an event ... 
        self.ignore = False
        
        # Finally, we need to set our GUI according to capabilities
      
    #=====================================================================
    # Entry point
    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event)
        gtk.main()
        # Return here when the UI is closed
        return self.quitall
